﻿using Moqikaka.DAL;
using Moqikaka.Model;
using Moqikaka.Util.Attr;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Moqikaka.BLL
{
    [BLLAttribute("Login")]
    public class LoginBLL
    {
        /// <summary>
        /// 某个玩家是否符合参加回归活动条件,刷新缓存
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="partnerId"></param>
        /// <returns></returns>
        public static JoinOldPlayerReturnCache RePlayerReturnCheck(string userId, int partnerId)
        {
            var cache = new JoinOldPlayerReturnCache();

            cache.UserId = userId;
            cache.PartnerId = partnerId;
            cache.CanJoinPlayerReturn = false;
            cache.JoinedPlayerReturn = true;

            LoginLog loginLog = new LoginLog();
            JoinOldPlayerReturn joinTimeRow = null;

            //检查符合玩家回归活动

            var loginLogTable = LoginLogDAL.GetLastLog(userId, partnerId);
            var joinTable = PlayerReturn.GetPlayerReturn(userId, partnerId);

            //从未登陆过，只有上次登录
            if (loginLogTable == null && loginLogTable.Rows.Count <= 0)
            {
                cache.CanJoinPlayerReturn = false;
                goto Save;
            }

            if (joinTable != null && joinTable.Rows.Count > 0)
            {
                joinTimeRow = new JoinOldPlayerReturn(joinTable.Rows[0]);
            }


            loginLog = new LoginLog(loginLogTable.Rows[0]);

            //上次离线时间 若是符合参加条件
            var offlineTimeSpan = DateTime.Now - loginLog.LoginTime;

            if (offlineTimeSpan > PlayerReturnConfig.MinTime &&
                offlineTimeSpan < PlayerReturnConfig.MaxTime)
            {
                cache.CanJoinPlayerReturn = true;
                if (joinTimeRow == null || joinTimeRow.LinkLastLoginTime > loginLog.LoginTime || joinTimeRow.IsUse)
                {
                    PlayerReturn.SetPlayerReturn(userId, partnerId, loginLog.LoginTime, false);
                    cache.JoinedPlayerReturn = false;
                }
                else
                {
                    cache.JoinedPlayerReturn = joinTimeRow.IsUse;
                }
            }
            else
            {
                if (joinTimeRow != null)
                {
                    cache.CanJoinPlayerReturn = !joinTimeRow.IsUse;
                    cache.JoinedPlayerReturn = joinTimeRow.IsUse;
                }
            }

        Save:
            //保存缓存
            PlayerReturnCache.Set(userId, partnerId, cache);
            return cache;
        }


        [BLLActionAttribute("UserLogin")]
        public static ResultResponse Login(string userId, int partnerId)
        {
            var result = new ResultResponse();

            //登录成功
            var testlist = new List<Player>()
            {
                new Player()
                {
                    UserId = "test1",
                    PartnerId = 12,
                    PlayerId = Guid.Parse("F6C3736D-B0B7-4753-AE99-FD3584CDBDE5")
                },
            };
            var player = testlist.Find(
                (item) => { return item.UserId == userId && item.PartnerId == partnerId; });
            if (player == null)
            {
                result.ResultCode = ResultCodeEnum.LoginFailure; //登录失败
                return result;
            }


            //保存本次登录日志
            LoginLogDAL.InsertLoginLog(player.UserId, player.PartnerId, player.PlayerId, DateTime.Now);

            RePlayerReturnCheck(userId, partnerId);

            result.ResultCode = ResultCodeEnum.LoginSuccess;
            return result;
        }
    }
}
